using System;

namespace Comteck.Winforms.Utils {
  /// <summary>
  /// 汉字转化
  /// </summary>
  public static class ChineseHelper {

    #region 汉字转拼音

    ///// <summary>
    ///// 多音字列表(定义常用首拼字母)
    ///// </summary>
    ///// <remarks>
    ///// 默认的PinYinConverter类获得的第一个首拼字母不适合时才需要手工定义
    ///// 添加格式：汉字 + 首拼字母，汉字不能重复
    ///// </remarks>
    //private static Dictionary<char, char> PolyphoneDefaultList = new Dictionary<char, char> {
    //    { '红', 'H' },
    //    { '石', 'S' },
    //    { '单', 'D' },
    //    { '茄', 'Q' },
    //    { '虾', 'X' },
    //  };

    /// <summary>
    /// 汉字转换成拼音
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ConvertToPinYin(string str) {
      string PYstr = "";
      foreach (char item in str.ToCharArray()) {
        if (Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(item)) {
          var cc = new Microsoft.International.Converters.PinYinConverter.ChineseChar(item);

          PYstr += cc.Pinyins[0].Substring(0, cc.Pinyins[0].Length - 1);
        } else {
          PYstr += item.ToString();
        }
      }
      return PYstr;
    }

    #endregion
    /// <summary>
    /// 计算两个坐标之间的距离 add by alex 
    /// </summary>
    /// <param name="lat1">坐标1纬度</param>
    /// <param name="lng1">坐标1经度</param>
    /// <param name="lat2">坐标2纬度</param>
    /// <param name="lng2">坐标2经度</param>
    /// <returns></returns>
    public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
      double radLat1 = Rad(lat1);
      double radLat2 = Rad(lat2);
      double a = radLat1 - radLat2;
      double b = Rad(lng1) - Rad(lng2);

      double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
       Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
      s = s *SmartRetailConstants.EARTH_RADIUS;
      s = Math.Round(s * 10000) / 10000;
      return Math.Round(s,2);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="d"></param>
    /// <returns></returns>
    private static double Rad(double d) {
      return d * Math.PI / 180.0;
    }
    /// <summary>
    /// 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标 即 高德转百度
    /// </summary>
    /// <param name="gd_lon"></param>
    /// <param name="gd_lat"></param>
    /// <returns></returns>
    public static (double bd_lat ,double bd_lng) GaoDeToBaidu(double gd_lng, double gd_lat) {
      var PI = 3.14159265358979324 * 3000.0 / 180.0;
      double x = gd_lng, y = gd_lat;
      double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * PI);
      double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * PI);
      var bd_lng= z * Math.Cos(theta) + 0.0065;
      var bd_lat = z * Math.Sin(theta) + 0.006;
      return (bd_lat, bd_lng);
    }
  }
}
